'use strict';
'require view';
'require fs';
'require rpc';
'require validation';
'require ui';
var callBeidouDataDump = rpc.declare({
	object: 'sihan.beidou',
	method: 'location',
	expect: {}
});
var callSetCmdDump = rpc.declare({
	object: 'sihan.beidou',
	method: 'cmd_ctl',
	params: ['cmd'],
	expect: {}
});
var callSaveCfgDump = rpc.declare({
	object: 'sihan.beidou',
	method: 'save_cfg',
	params: [],
	expect: {}
});
function getBeidouData() {
	return L.resolveDefault(callBeidouDataDump(), {})
		.then(function(res) {
			console.log(res);
			return res;
		});
}
function renderBeidouData(res) {
	var renderHTML = "";
	var spanTemp = '<ul><li>时间: %s</li><li>经度: %s</li><li>维度: %s</li><li>海拔: %s</li><li>定位质量: %d</li>'
	spanTemp += '<li>卫星数量: %d</li><li>水平精度因子: %s</li></ul>';
	renderHTML += String.format(spanTemp, res.time, res.latitude, res.longitude, res.altitude, res.fix_quality, res.satellites, res.hdop);
	return renderHTML;
}
return view.extend({
	load: function() {
		// return getDevs();
	},
	buttonClick: function(){
		var PCAS03 = document.querySelector('input[id="PCAS03"]').value;
		console.log(PCAS03);
		Promise.all([callSetCmdDump(PCAS03),callSaveCfgDump]);
		
	},
	render: function(data) {
		
		var table = E('table', {
			'class': 'table assoclist',
			'id': 'wifi_assoclist_table'
		}, [
			E('span',{},"命令帧格式说明"),
			E('div',{},"命令格式：$PCAS03,<GNGGA>,<GNGLL>,<GPGSA/BDGSA>,<GPGSV/BDGSV>,<GNRMC>,<GNVTG>,<GNZDA>,<GPTXT>,<>*<Checksum><CR><LF>"),
			E('div',{},"示例:PCAS03,9,0,0,0,0,0,9,0,0,0,0,0,0,0,0,0,0,0"),
			E('div',{},"保存配置：$PCAS00*CS"),
			E('div',{},"示例:PCAS00"),
			E('div',{},"更新频率：$PCAS02,fixInt*CS"),
			E('div',{},"示例:PCAS02,1000"),
			E('div',{},[
				E('span',{},"PCAS指令:"),
				E('input',{'style':'width:270px;margin: 0 15px 0 15px','id':"PCAS03"},''),
				E('button',{
					'class': 'cbi-button cbi-button-apply',
					'click': L.bind(this.buttonClick, this, null)
				},[_('保存')])
			]),
		]);
		var beidou_info = function(section_id) {
			L.Poll.add(function() {
				return L.resolveDefault(getBeidouData())
					.then(function(res) {
						var view = document.getElementById("beidou_data");
						view.innerHTML = renderBeidouData(res);
					});
			});
			return E('div', {
				class: 'cbi-map'
			}, E('fieldset', {
				class: 'cbi-section'
			}, [E('p', {
				id: 'beidou_data'
			}, _('Collecting data ...'))]));
		}
		return E([E('h3', _('北斗接收系统配置')), table,E('h3', _('北斗实时数据')),beidou_info()]);
	},
	handleSaveApply: null,
	handleSave: null,
	handleReset: null
});